{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demo for XYZ Spaces for Python\n",
    "\n",
    "This notebook shows a demo of xyzspaces."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This is only to show what to integrate better into Jupyter in the future\n",
    "# (not covered further in this notebook).\n",
    "\n",
    "from IPython.display import IFrame\n",
    "\n",
    "url = \"https://xyz.here.com/viewer/\"\n",
    "project_id = \"5c54716d-f900-4b89-80ac-b21518e94b30\"\n",
    "IFrame(src=f\"{url}?project_id={project_id}\", width=\"100%\", height=400)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import requests\n",
    "import warnings\n",
    "from sidecar import Sidecar\n",
    "from ipyleaflet import Map, GeoJSON, FullScreenControl\n",
    "\n",
    "from xyzspaces.apis import HubApi\n",
    "from xyzspaces.datasets import get_countries_data\n",
    "import xyzspaces"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-warning\">\n",
    "<b>Warning:</b> Before running below cells please make sure you have XYZ Token to interact with xyzspaces. \n",
    "                Please see README.md in notebooks folder for more info on XYZ_TOKEN\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Make a XYZ object\n",
    "try:\n",
    "    xyz_token = os.environ[\"XYZ_TOKEN\"]\n",
    "except KeyError:\n",
    "    xyz_token = \"MY-FANCY-XYZ-TOKEN\"\n",
    "    if xyz_token == \"MY-FANCY-XYZ-TOKEN\":\n",
    "        warnings.warn(\n",
    "            \"Please either set XYZ_TOKEN to env variable or \"\n",
    "            \"just assign value of your actual token to variable xyz_token above.\"\n",
    "        )\n",
    "xyz = xyzspaces.XYZ(credentials=xyz_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "title = \"My Demo Space\"\n",
    "description = \"Demo\"\n",
    "space = xyz.spaces.new(title=title, description=description)\n",
    "space_id = space.info[\"id\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create map widget in sidebar (JupyterLab needed)\n",
    "m = Map(zoom=2)\n",
    "with Sidecar(title='XYZ'):\n",
    "    display(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json\n",
    "# with minor clean-ups\n",
    "gj = get_countries_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# add world GeoJSON to XYZ space (returns the added GeoJSON)\n",
    "\n",
    "world = space.add_features(features=gj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# add entire GeoJSON feature from XYZ to the map\n",
    "m += GeoJSON(data=world)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# add \"full screen\" button to map\n",
    "m += FullScreenControl()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fra = space.get_feature(feature_id='FRA')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# add the feature to the map\n",
    "style = dict(\n",
    "    color=\"red\",\n",
    "    opacity=1,\n",
    "    weight=1.9, \n",
    "    fillOpacity=0.5\n",
    ")\n",
    "m += GeoJSON(data=fra, style=style)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# show only countries on southern hemisphere \n",
    "\n",
    "from geojson import FeatureCollection\n",
    "\n",
    "bbox = [-180, -90, 180, 0]\n",
    "bfeatures = []\n",
    "for feature in space.features_in_bbox(bbox=[0, 0, 20, 20]):\n",
    "    bfeatures.append(feature)\n",
    "hemi = FeatureCollection(bfeatures)\n",
    "m += GeoJSON(data=hemi, style=style)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# show the same but with clipped borders \n",
    "for feature in space.features_in_bbox(bbox=[0, 0, 20, 20], clip=True):\n",
    "    bfeatures.append(feature)\n",
    "hemi = FeatureCollection(bfeatures)\n",
    "style[\"color\"] = \"yellow\"\n",
    "m += GeoJSON(data=hemi, style=style)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Spatial search: Given latitude and longitude search intersecting geometry.\n",
    "\n",
    "fc = []\n",
    "for feature in space.spatial_search(lat=19.1526, lon=72.8557, radius=100000):\n",
    "    fc.append(feature)\n",
    "    \n",
    "spatial_search = FeatureCollection(fc)\n",
    "style[\"color\"] = \"orange\"\n",
    "m += GeoJSON(data=spatial_search, style=style)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# clean-up and delete temporary space\n",
    "space.delete()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
